package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author He Wentao
 * @since 2022/9/13 21:44
 */

@RestController
@RequestMapping("/eduservice/indexfront")
public class IndexFrontController {

    @Resource
    EduCourseService courseService;

    @Resource
    EduTeacherService teacherService;


    @Cacheable(value = "index", key = "'indexInfo'")
    @GetMapping("index")
    public R getAllTeacher(){
        QueryWrapper<EduCourse> courseWrapper = new QueryWrapper<>();
        // 根据浏览量进行排序，并且获取到前 8 个课程
        courseWrapper.orderByDesc("view_count");
        // 拼接 sql 只查询出八条记录，存在注入风险
        courseWrapper.last("limit 4");
        // 保存热门课程信息
        List<EduCourse> courseList = courseService.list(courseWrapper);


        QueryWrapper<EduTeacher> teacherWrapper = new QueryWrapper<>();
        // 根据id进行排序，并且获取到前 4 个讲师
        teacherWrapper.orderByDesc("sort");
        // 拼接 sql 只查询出四条记录，存在注入风险
        teacherWrapper.last("limit 4");
        List<EduTeacher> teacherList = teacherService.list(teacherWrapper);

        // 返回热门课程
        return R.ok().data("courseList",courseList).data("teacherList",teacherList);
    }

}
